package uk.co.lukeparker;

import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;

public class JazzCommandExecutor implements CommandExecutor{
	
	private JazzCraft plugin;

	public JazzCommandExecutor(JazzCraft plugin){
		this.plugin = plugin;
	}
	
	@Override
	public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
		Player player = (Player) sender;
		FileConfiguration config = plugin.getConfig();
		if(sender instanceof Player){
			
			// Set Lobby Spawn
			if(cmd.getName().equalsIgnoreCase("setlobbyspawn")){
				if(player.isOp()){
					config.set("Lobby Spawn.world", player.getWorld().getName());
					config.set("Lobby Spawn.x", player.getLocation().getX());
					config.set("Lobby Spawn.y", player.getLocation().getY());
					config.set("Lobby Spawn.z", player.getLocation().getZ());
					plugin.saveConfig();
					sender.sendMessage(JazzCraft.messageTemplate("Lobby Spawn set to your position!", ChatColor.AQUA));
					return true;
				}else{
					sender.sendMessage(JazzCraft.messageTemplate("You must be op to do this!", ChatColor.RED));
				}
			}
			
			// Set Team1 Spawn
			if(cmd.getName().equalsIgnoreCase("setteam1spawn")){
				if(player.isOp()){
					config.set("Team1 Spawn.world", player.getWorld().getName());
					config.set("Team1 Spawn.x", player.getLocation().getX());
					config.set("Team1 Spawn.y", player.getLocation().getY());
					config.set("Team1 Spawn.z", player.getLocation().getZ());
					plugin.saveConfig();
					sender.sendMessage(JazzCraft.messageTemplate("Team 1 Spawn set to your position!", ChatColor.AQUA));
					return true;
				}else{
					sender.sendMessage(JazzCraft.messageTemplate("You must be op to do this!", ChatColor.RED));
				}
			}
			
			// Set Team2 Spawn
			if(cmd.getName().equalsIgnoreCase("setteam2spawn")){
				if(player.isOp()){
					config.set("Team2 Spawn.world", player.getWorld().getName());
					config.set("Team2 Spawn.x", player.getLocation().getX());
					config.set("Team2 Spawn.y", player.getLocation().getY());
					config.set("Team2 Spawn.z", player.getLocation().getZ());
					plugin.saveConfig();
					sender.sendMessage(JazzCraft.messageTemplate("Team 2 Spawn set to your position!", ChatColor.AQUA));
					return true;
				}else{
					sender.sendMessage(JazzCraft.messageTemplate("You must be op to do this!", ChatColor.RED));
				}
			}
			
			// Join Team
			if(cmd.getName().equalsIgnoreCase("join")){
				if(args.length < 1 || args.length > 1){
					sender.sendMessage(JazzCraft.messageTemplate("Wrong number of args!", ChatColor.RED));
					return true;
				}else{
					if(args[0].equalsIgnoreCase(config.getString("Team Names.1"))){
						if(!JazzCraft.team1.contains(player.getName()) && !JazzCraft.team2.contains(player.getName())){
							JazzCraft.team1.add(player.getName());
							sender.sendMessage(JazzCraft.messageTemplate("You joined " + config.getString("Team Names.1"), ChatColor.GREEN));
						}else{
							sender.sendMessage(JazzCraft.messageTemplate("You are already on a team!", ChatColor.RED));
							return true;
						}
						return true;
					}
					if(args[0].equalsIgnoreCase(config.getString("Team Names.2"))){
						if(!JazzCraft.team1.contains(player.getName()) && !JazzCraft.team2.contains(player.getName())){
							JazzCraft.team2.add(player.getName());
							sender.sendMessage(JazzCraft.messageTemplate("You joined " + config.getString("Team Names.2"), ChatColor.GREEN));
						}
						return true;
					}
					if(!args[0].equalsIgnoreCase(config.getString("Team Names.1")) && !args[0].equalsIgnoreCase(config.getString("Team Names.2"))){
						sender.sendMessage(JazzCraft.messageTemplate("Team doesn't exist!", ChatColor.RED));
						return true;
					}
				}
			}
			
			// Leave Team
			if(cmd.getName().equalsIgnoreCase("leave")){
				if(JazzCraft.team1.contains(player.getName())){
					JazzCraft.team1.remove(player.getName());
					if(config.getInt("Lobby Spawn.x") != 0 || config.getInt("Lobby Spawn.y") != 0 || config.getInt("Lobby Spawn.z") != 0){
						player.teleport(new Location(plugin.getServer().getWorld(config.getString("Lobby Spawn.world")), config.getInt("Lobby Spawn.x"), config.getInt("Lobby Spawn.y"), config.getInt("Lobby Spawn.z")));
					}
					player.sendMessage(JazzCraft.messageTemplate("You left " + config.getString("Team Names.1"), ChatColor.AQUA));
					return true;
				}
				if(JazzCraft.team2.contains(player.getName())){
					JazzCraft.team2.remove(player.getName());
					if(config.getInt("Lobby Spawn.x") != 0 || config.getInt("Lobby Spawn.y") != 0 || config.getInt("Lobby Spawn.z") != 0){
						player.teleport(new Location(plugin.getServer().getWorld(config.getString("Lobby Spawn.world")), config.getInt("Lobby Spawn.x"), config.getInt("Lobby Spawn.y"), config.getInt("Lobby Spawn.z")));
					}
					player.sendMessage(JazzCraft.messageTemplate("You left " + config.getString("Team Names.2"), ChatColor.AQUA));
					return true;
				}
				if(!JazzCraft.team1.contains(player.getName()) && !JazzCraft.team2.contains(player.getName())){
					player.sendMessage(JazzCraft.messageTemplate("You are not in a team!", ChatColor.RED));
					return true;
				}
			}
			
		}else{
			sender.sendMessage("You Must be a Player!");
			return false;
		}
		return false;
	}

}
